﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.EF;
using System.Web.Security;

namespace PNProject.Controllers
{
    [Authorize]
    public class AdminLinkController : Controller
    {
        //
        // GET: /OneContent/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult TextLink()
        {
            List<LinkModel> result = new List<LinkModel>();
            var links = StaticData.linkRepo.GetSome(a => a.Type == true);
            foreach (var item in links)
            {
                LinkModel link = new LinkModel();
                link.LinkID = item.LinkID;
                link.TextLink = string.IsNullOrEmpty(item.TextLink) || string.IsNullOrEmpty(item.URL) ? "Lỗi link" : item.TextLink;
                link.URL = item.URL;
                result.Add(link);
            }
            return View(result);
        }

        public ActionResult ImageLink()
        {
            List<LinkModel> result = new List<LinkModel>();
            var links = StaticData.linkRepo.GetSome(a => a.Type == false);
            foreach (var item in links)
            {
                LinkModel link = new LinkModel();
                link.LinkID = item.LinkID;
                link.URL = string.IsNullOrEmpty(item.Image) || string.IsNullOrEmpty(item.URL) ? "Lỗi link" : item.URL;
                link.Image = item.Image;
                result.Add(link);
            }
            return View(result);
        }

        public ActionResult Create()
        {            
            return View(new LinkModel());
        }

        [HttpPost]
        public ActionResult Create(LinkModel model, HttpPostedFileBase file)
        {
            Link link = new Link();
            link.URL = model.URL;
            link.Lang = model.Lang;
            if (model.Type)
            {
                link.TextLink = model.TextLink;
                link.Type = true;
            }
            else
            {
                link.Type = false;
                if (string.IsNullOrEmpty(model.Image))
                {
                    if (file != null)
                    {
                        var legal = file.ContentType == "image/jpeg" || file.ContentType == "image/png" || file.ContentType == "image/gif" || file.ContentType == "image/jpg";
                        if (legal)
                        {
                            string time = PNLibraries.PNConvertToTimestamp(DateTime.Now).ToString();
                            string newName = time + "-" + file.FileName;
                            string path = "/Application_Data/Images/LinkImages/";
                            ServerHelper.SaveFileBaseOnServer(path, file, newName);
                            link.Image = path + newName;
                        }
                    }
                }
                else
                {
                    link.Image = model.Image;
                }
            }
            StaticData.linkRepo.Save(null, link);
            return RedirectToAction("Index");
        }

        public ActionResult Delete(string id)
        {
            try
            {
                int newid = int.Parse(id);
                var dbo = StaticData.linkRepo.GetOne(a => a.LinkID == newid);
                ServerHelper.DeleteFileOnServer(dbo.Image);
                StaticData.linkRepo.Delete(dbo);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return RedirectToAction("Error", "Admin");
            }
        }

        public ActionResult Edit(string id)
        {
            try
            {
                int newid = int.Parse(id);
                var dbo = StaticData.linkRepo.GetOne(a => a.LinkID == newid);
                var conv = LinkConverter.LinkTo(dbo);
                return View(conv);
            }
            catch (Exception e)
            {
                return RedirectToAction("Error", "Admin");
            }
        }

        [HttpPost]
        public ActionResult Edit(LinkModel model, HttpPostedFileBase file)
        {
            Link dbo = new Link();
            try
            {
                dbo = StaticData.linkRepo.GetOne(a => a.LinkID == model.LinkID);
            }
            catch (Exception e)
            {
                return RedirectToAction("Error", "Admin");
            }

            Link link = new Link();
            link.URL = model.URL;
            link.LinkID = dbo.LinkID;
            link.Lang = model.Lang;
            if (model.Type)
            {
                ServerHelper.DeleteFileOnServer(dbo.URL);
                link.TextLink = model.TextLink;
                link.Type = true;
            }
            else
            {
                link.Type = false;
                if (string.IsNullOrEmpty(model.Image))
                {
                    if (file != null)
                    {
                        var legal = file.ContentType == "image/jpeg" || file.ContentType == "image/png" || file.ContentType == "image/gif" || file.ContentType == "image/jpg";
                        if (legal)
                        {
                            ServerHelper.DeleteFileOnServer(dbo.Image);
                            string time = PNLibraries.PNConvertToTimestamp(DateTime.Now).ToString();
                            string newName = time + "-" + file.FileName;
                            string path = "/Application_Data/Images/LinkImages/";
                            ServerHelper.SaveFileBaseOnServer(path, file, newName);
                            link.Image = path + newName;
                        }
                    }
                    else
                    {
                        ServerHelper.DeleteFileOnServer(dbo.Image);
                    }
                }
                else
                {
                    if(dbo.Image!=model.Image)
                        ServerHelper.DeleteFileOnServer(dbo.Image);
                    link.Image = model.Image;
                }
            }
            StaticData.linkRepo.Save(dbo, link);
            return RedirectToAction("Index");
        }       

    }
}
